package others.swing.p022;

import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MyService {
    private static MyService service = null;
    private File currentFile = null;
    private File currentDirectory = null;
    private List<File> listCurrentFile = null;
    private JFileChooser fileChooser = new JFileChooser();
    private double largeRate = 0.2;

    public void openFile(MyFrame frame) {
        //设置文件的简单过滤
        FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "png", "gif");
        fileChooser.addChoosableFileFilter(filter);
        fileChooser.showOpenDialog(frame);
        fileChooser.addChoosableFileFilter(filter);

        this.currentFile = fileChooser.getSelectedFile();
        String fileImgName = this.currentFile.getPath();
        if (fileImgName.isEmpty()) {
            return;
        }
        File fileDirectory = fileChooser.getCurrentDirectory();
        //接下来将所有的文件
        if (currentDirectory != fileDirectory) {
            File files[] = fileDirectory.listFiles();
            this.listCurrentFile = new ArrayList<File>();
            for (File file : files) {
                String str = file.getName();
                if (str.endsWith("png") || str.endsWith("jpg") || str.endsWith("gif")) {
                    this.listCurrentFile.add(file);
                }
            }
        }
        ImageIcon Img = new ImageIcon(fileImgName);
        frame.getLabel().setIcon(Img);
    }

    public void lastImage(MyFrame frame) {
        if (this.listCurrentFile != null && !this.listCurrentFile.isEmpty()) {
            int index = this.listCurrentFile.indexOf(this.currentFile);
            if (index > 0) {
                File file = (File) this.listCurrentFile.get(index - 1);
                ImageIcon img = new ImageIcon(file.getPath());
                frame.getLabel().setIcon(img);
                this.currentFile = file;
            } else {
                return;
            }
        } else {
            return;
        }
    }

    public void nextwImage(MyFrame frame) {
        int lenOfList = listCurrentFile.size();
        if (this.listCurrentFile != null && !this.listCurrentFile.isEmpty()) {
            int index = this.listCurrentFile.indexOf(this.currentFile);
            if (index + 1 < lenOfList) {
                File file = (File) this.listCurrentFile.get(index + 1);
                ImageIcon Img = new ImageIcon(file.getPath());
                frame.getLabel().setIcon(Img);
                this.currentFile = file;
            } else {
                return;
            }
        } else {
            return;
        }
    }

    public void zoomBigger(MyFrame frame) {
        ImageIcon img = (ImageIcon) frame.getLabel().getIcon();
        if (img != null) {
            int width = (int) (img.getIconWidth() * (1 + largeRate));
            ImageIcon newImg = new ImageIcon(img.getImage().getScaledInstance(width, -1, Image.SCALE_DEFAULT));
            frame.getLabel().setIcon(newImg);
        } else {
            return;
        }
    }

    public void zoomSmall(MyFrame frame) {
        ImageIcon img = (ImageIcon) frame.getLabel().getIcon();
        if (img != null) {
            int width = (int) (img.getIconWidth() * (1 - largeRate));
            ImageIcon newImg = new ImageIcon(img.getImage().getScaledInstance(width, -1, Image.SCALE_DEFAULT));
            frame.getLabel().setIcon(newImg);
        } else {
            return;
        }
    }

    public static MyService getInstance() {
        if (service == null) {
            service = new MyService();
        }
        return service;
    }

    public void funcOfImage(MyFrame frame, String cmd) {
        if (cmd.equals("打开")) {
            openFile(frame);
        } else if (cmd.equals("退出")) {
            System.exit(0);
        } else if (cmd.equals("上一页")) {
            lastImage(frame);
        } else if (cmd.equals("下一页")) {
            nextwImage(frame);
        } else if (cmd.equals("放大")) {
            zoomBigger(frame);
        } else if (cmd.equals("缩小")) {
            zoomSmall(frame);
        } else {
            return;
        }
    }
}
